package utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class C3P0Utils{
    //创建唯一的C3P0连接池对象
    private static final ComboPooledDataSource DATASOURCE=new ComboPooledDataSource();

    /**
     * 获得连接池的方法
     * @return
     */
    public static DataSource getDataSources(){
        return DATASOURCE;
    }

    /**
     * 获得连接
     * @return
     * @throws SQLException
     */
    public static Connection getConnection()throws SQLException{
        Connection connection = DATASOURCE.getConnection();
        return connection;
    }
    /**
     * 释放资源
     * @param resultSet
     * @param statement
     * @param connection
     */
    public static void release(ResultSet resultSet, Statement statement, Connection connection){
        if(resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(statement!=null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }
}
